using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace JingQiao.Ads.Infrastructure.Data.NH.DomainObject
{
    using Domain.Model.DomainObject;

    public partial class Advertisement : IAdvertisement
    {
        #region Primitive Properties
    
        public virtual int AdvertisementId
        {
            get;
            set;
        }
    
        public virtual string Title
        {
            get;
            set;
        }
    
        public virtual byte Status
        {
            get;
            set;
        }
    
        public virtual byte Type
        {
            get;
            set;
        }
    
        public virtual string LinkUrl
        {
            get;
            set;
        }
    
        public virtual string UnionAdHtml
        {
            get;
            set;
        }
    
        public virtual string AttachmentUrl
        {
            get;
            set;
        }
    
        public virtual int Sequence
        {
            get;
            set;
        }
    
        public virtual int UserId
        {
            get;
            set;
        }
    
        public virtual string Creator
        {
            get;
            set;
        }
    
        public virtual System.DateTime CreateDate
        {
            get;
            set;
        }

        #endregion
        #region Navigation Properties
    
        public virtual ICollection<IDestPositionAd> DestPositionAd
        {
            get
            {
                if (_destPositionAd == null)
                {
                    var newCollection = new FixupCollection<IDestPositionAd>();
                    newCollection.CollectionChanged += FixupDestPositionAd;
                    _destPositionAd = newCollection;
                }
                return _destPositionAd;
            }
            set
            {
                if (!ReferenceEquals(_destPositionAd, value))
                {
                    var previousValue = _destPositionAd as FixupCollection<IDestPositionAd>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupDestPositionAd;
                    }
                    _destPositionAd = value;
                    var newValue = value as FixupCollection<IDestPositionAd>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupDestPositionAd;
                    }
                }
            }
        }
        private ICollection<IDestPositionAd> _destPositionAd;

        #endregion
        #region Association Fixup
    
        private void FixupDestPositionAd(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (IDestPositionAd item in e.NewItems)
                {
                    item.Advertisement = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (IDestPositionAd item in e.OldItems)
                {
                    if (ReferenceEquals(item.Advertisement, this))
                    {
                        item.Advertisement = null;
                    }
                }
            }
        }

        #endregion
    }
}
